//
//  XXZXLBViewController.m
//  微客
//
//  Created by 汪的一声就哭了！ on 2016/11/15.
//  Copyright © 2016年 刘成. All rights reserved.
//

#import "XXZXLBViewController.h"

@interface XXZXLBViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation XXZXLBViewController
{
    UITableView *XXZXtableView;
    NSMutableArray *data;
    //设置分页变量
    NSInteger page;

}
- (void)obtainData
{
    [self obtainListDataWithHud:NO];
}
- (void)obtainListDataWithHud:(BOOL)hud
{
    page = 1;
    NSString *str;
    if ([self.titleStr isEqualToString:@"系统消息"]) {
        str = @"message";
    } else if ([self.titleStr isEqualToString:@"任务消息"]) {
        str = @"taskmessage";
    }
    [NetRequiest handleDataWithString:HTTPLOAD dictionary:@{@"v1":@"message",@"uid":UID,@"star":str,@"page":[NSString stringWithFormat:@"%ld",page]} HUD:hud block:^(id responseObject) {
        [XXZXtableView.mj_header endRefreshing];
        data = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"result"]) {
            MessageModel *model = [[MessageModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [data addObject:model];
        }
        [XXZXtableView reloadData];
        page++;
    }];
}
- (void)obtainMoreData
{
    NSString *str;
    if ([self.titleStr isEqualToString:@"系统消息"]) {
        str = @"message";
    } else if ([self.titleStr isEqualToString:@"任务消息"]) {
        str = @"taskmessage";
    }
    [NetRequiest handleDataWithString:HTTPLOAD dictionary:@{@"v1":@"message",@"uid":UID,@"star":str,@"page":[NSString stringWithFormat:@"%ld",page]} HUD:NO block:^(id responseObject) {
        [XXZXtableView.mj_footer endRefreshing];
        for (NSDictionary *dic in responseObject[@"result"]) {
            MessageModel *model = [[MessageModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [data addObject:model];
        }
        [XXZXtableView reloadData];
        page++;
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self obtainListDataWithHud:YES];
    //设置按钮返回样式
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    self.title = self.titleStr;
    XXZXtableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStylePlain];
    XXZXtableView.dataSource = self;
    XXZXtableView.delegate = self;
    [XXZXtableView registerNib:[UINib nibWithNibName:@"XXZXTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    XXZXtableView.backgroundColor = BackColor;
    [XXZXtableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    XXZXtableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    XXZXtableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 3)];
    [self.view addSubview:XXZXtableView];

    //调用下拉刷新
    [self setupRefresh];
}

//下拉刷新
- (void)setupRefresh
{
    XXZXtableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(obtainData)];

    // 自动改变透明度
    XXZXtableView.mj_header.automaticallyChangeAlpha = YES;
    // 马上进入刷新状态
    [XXZXtableView.mj_header beginRefreshing];

    XXZXtableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(obtainMoreData)];
}

//设置表视图高度
- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 120;
}

//设置表视图中的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return data.count;
    
}

//设置单元格中的数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XXZXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    if ([data[indexPath.row] m_name]) {
        [cell.XXZXIV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[data[indexPath.row] headimage]]] placeholderImage:[UIImage imageNamed:@"占位图"]];
        cell.XXZXL1.text = [data[indexPath.row] m_name];
        cell.XXZXL2.text = [data[indexPath.row] m_time];
        cell.XXZXL3.text = [data[indexPath.row] m_content];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    XXZXXQViewController *viewC = [[XXZXXQViewController alloc] init];
    [viewC setHidesBottomBarWhenPushed:YES];
    viewC.model = data[indexPath.row];
    [self.navigationController pushViewController:viewC animated:YES];
}
#pragma mark ---------- 时间戳
- (id)timeStamp:(NSString *)timeStr{
    NSInteger a=[timeStr integerValue];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:a];
    NSString *timeString = [formatter stringFromDate:confromTimesp];
    
    
    NSTimeInterval secondsPerDay = 224 * 660 * 60;
    NSDate *today = [NSDate date];
    NSDate *theDayBeforeYestoday, *yesterday;
    
    theDayBeforeYestoday = [today dateByAddingTimeInterval: -secondsPerDay * 2];
    yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
    
    
    NSString *todayString = [[today description] substringToIndex:10];
    NSString *yesterdayString = [[yesterday description] substringToIndex:10];
    NSString *tomorrowString = [[theDayBeforeYestoday description] substringToIndex:10];
    NSString *thisYearString = [[today description] substringToIndex:4];
    
    NSString * dateString = [[confromTimesp description] substringToIndex:10];
    
    if ([dateString isEqualToString:todayString]) {
        return @"今天";
    } else if ([dateString isEqualToString:yesterdayString]) {
        return @"昨天";

    }else if ([dateString isEqualToString:tomorrowString]) {

    } else {
        if ([[dateString substringToIndex:4] isEqualToString:thisYearString]) {
            
        } else {
        }
    }
    return timeString;
}

@end
